<template>
	<el-dialog title="添加学生" :visible="true" :modal-append-to-body="false" :show-close="false" size="large">
        <i class="el-icon-close close" @click="cancel"></i>
		
        <div class="box cf">
        	<div class="left">
	            <div class="search">
	                <el-input class="input" v-model="vkeyword" placeholder="学生查询" icon="search" @keydown.enter.native="search"></el-input>
	                <el-button type="info" class="btn" @click="search">搜索</el-button>
	            </div>

	            <!-- 数据表格展示区域 -->
	            <el-table 
	                :data="dataList" 
	                ref="multipleTable"
	                border
	                align="center" 
	                class="admin-table"
	                row-key="id"
	                @select="checkSelect" 
	                @select-all="checkall">

	                <el-table-column 
	                    type="selection" 
	                    width="55" 
	                    :reserve-selection="true"></el-table-column>
	                

	                <el-table-column 
	                    :prop="v.prop" 
	                    :label="v.label" 
	                    v-for="(v,index) in portTitleList" 
	                    align="center" 
	                    show-overflow-tooltip 
	                    v-if="v.hide != 0"  
	                    :formatter="v.formatter"
	                    :align="v.align || 'center'"
	                    :fixed="v.fixed"
	                    :class-name="v.columnStyle"
	                    :width="v.width"
	                    :min-width="v.minWidth"
	                    :header-align="v.headerAlign || 'center'"
	                    >
	                    <template scope="scope">  
	                        <div slot="reference"> 
		                        <img 
		                        :src="scope.row.xb == '男' ? $store.state.sexImg.boy : $store.state.sexImg.girl" 
		                        style="width:20px;vertical-align:middle;" 
		                        v-if="scope.column.property == 'zwm'">
		                        <span style="vertical-align:middle;">{{ scope.row[scope.column.property] }}</span>
		                    </div>
	                     </template>  
	                </el-table-column>
	            </el-table>

	            <!-- 分页查询区域 -->
	            <el-pagination
	                @size-change="handleSizeChange"
	                @current-change="handleCurrentChange"
	                :current-page.sync="pageindex"
	                :page-sizes="pagesizes"
	                :page-size="pagesize"
	                :layout="layout"
	                :total="total"
	                class="class-page">
	            </el-pagination>
	        </div>

	        <div class="right">
	            <div class="choose">
	            	<!-- <div class="first">
	            		<div>
                            <span>已选择:</span>
                            <span class="number">{{multipleList.length}}</span>
                        </div>
                        <span class="del el-icon-delete" @click="del()">
                            <i></i>
                        </span>
	            	</div> -->
	                <ul class="header">
	                    <li class="first">
	                        <div>
	                            <span>已选择:</span>
	                            <span class="number">{{multipleList.length}}</span>
	                        </div>
	                        <span class="del el-icon-delete" @click="del()">
	                            <i></i>
	                        </span>
	                    </li>
	                    <li v-for="(v,index) in multipleList">
	                        <span class="name ellipsis">{{v.zwm}}</span>
	                        <span class="del el-icon-delete" @click="del(multipleList[index])">
	                            <i></i>
	                        </span>
	                    </li>
	                </ul>
	            </div>
	        </div>
        </div>
		
		<el-button class="submit" @click="submit">添加</el-button>

	</el-dialog>
</template>

<script>
	import qs from 'qs'
    export default {
        props: ['params'],
        data () {
            return {
                vkeyword:'',
                multipleList: [],
                dataList: [],  //表格数据 

                /*
                    prop :  接口参数名
                    label : 接口中文名
                    hide : 是否隐藏这一列
                    align : 这一列对齐顺序 ： left,right,center
                    headerAlign : 这一列表头对齐方式 ： left,right,center
                */
                portTitleList: [
                    {
                        prop:'id',
                        label:'ID',
                        hide:0,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    },
                    {
                        prop:'zwm',
                        label:'中文名',
                        hide:1,
                        align:'left',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'left',
                        formatter: function(row,column) {}
                    },
                    {
                        prop:'csrq',
                        label:'出生日期',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    },
                    {
                        prop:'dh',
                        label:'电话',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    },
                    {
                        prop:'wx',
                        label:'微信',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    }
                   
                ],

                /*动画加载*/
                isLoading: false,//动画加载
                searchLoading: false,//搜索按钮触发按钮动画

                /*模糊搜索条件*/
                inputmc:'' , //  班级名称,

                /*分页属性设置*/
                layout: 'total, sizes, prev, pager, next, jumper',//组件布局
                pageindex: 1,//当前分页页码
                pagesizes: [12, 24, 36, 72],//显示个数的选择器选项
                pagesize: 12,//每页显示条目数
                total: 0,//总条数
                
                dropDownData:'', //
            }
        },
        created () {
                 
            //加载页面时调用
            this.search();//分页获取默认数据

        },
        mounted () {
            //开始了哦
        },
        methods: {
            
            search () {//查询搜索触发事件
            	var logininfo = JSON.parse(localStorage.logininfo);
              this.searchLoading = true;
             var params2 = qs.stringify({
                      keyword: this.vkeyword+'',
                        pageindex: this.pageindex,
                        pagesize: this.pagesize,
                        yhid:logininfo.id,
                        xxid:logininfo.xxid
               })
             //执行
                 this.axios.post(this.$store.state.api + 'xs/listxs',  params2 )
                 .then(res => {
                  console.log(res)
                    let data = res.data.data;
                    let list = data.list;
                    this.total = data.totalPage;
                    this.dataList = list;
                    this.searchLoading = false;
                  this.searchLoading = false;
                 }).catch(res => {
					 console.log(res)
                  this.searchLoading = false;
				})
            	
            	
            	
            	
            	
//              this.searchLoading = true;
//              var logininfo = JSON.parse(localStorage.logininfo);
//              this.axios.get(this.$store.state.api + 'xs/listxs',{
//                  params:{
//                  	keyword: this.vkeyword+'',
//                      pageindex: this.pageindex,
//                      pagesize: this.pagesize,
//                      yhid:logininfo.id,
//                      xxid:logininfo.xxid
//                  }
//              }).then( res => {
//                  console.log(res)
//                  let data = res.data.data;
//                  let list = data.list;
//                  this.total = data.totalPage;
//                  this.dataList = list;
//                  this.searchLoading = false;
//              }).catch( res => {
//                  console.log(res)
//                  this.searchLoading = false;
//              })
            },
            
            submit () {
//              let arr = [];
//                 this.multipleList.forEach( v => {
//                     arr.push(v.id)
//                 })
//                 this.multipleList = arr.join(',');
                this.multipleList.forEach( v => {
             var logininfo = JSON.parse(localStorage.logininfo);
             var params2 = qs.stringify({
                       yhid:logininfo.id+'',
                       xxid:logininfo.xxid+'',
                       bjid: this.params.row.id+'',
                       xsids: v.id+''
               })
             //执行
                 this.axios.post(this.$store.state.api + 'bj/updatexs',  params2 )
                 .then(res => {
                  console.log(res)
                        if (res.data.code == 1) {
                            this.$emit('close',{type:'add',cancel:false})
                        }else{
                            this.$emit('close',{type:'add',cancel:false})
                        }
                 }).catch(res => {
					 console.log(res);
                        this.$emit('close',{type:'add',cancel:false})
				})
            })



//              this.multipleList.forEach( v => {
//              	var logininfo = JSON.parse(localStorage.logininfo);
//                  this.axios.get(this.$store.state.api + 'bj/updatexs',{
//                      params:{
//                      	   yhid:logininfo.id+'',
//                             xxid:logininfo.xxid+'',
//                             bjid: this.params.row.id+'',
//                             xsids: v.id+''
//                      }
//                  }).then( res => {
//                      console.log(res)
//                      if (res.data.code == 1) {
//                          this.$emit('close',{type:'add',cancel:false})
//                      }else{
//                          this.$emit('close',{type:'add',cancel:false})
//                      }
//                  }).catch( res => {
//                      console.log(res);
//                      this.$emit('close',{type:'add',cancel:false})
//                  })
//              })

                // this.axios.get(this.$store.state.api + 'xs/addbj',{
                //     params:{
                //         bjid: this.params.row.id,
                //         xsid: this.multipleList
                //     }
                // }).then( res => {
                //     console.log(res)
                // }).catch( res => {
                //     console.log(res)
                // })
            },

            del (obj) {
                if (obj) {
                    this.$refs.multipleTable.toggleRowSelection(obj,false);
                }else{
                    this.$refs.multipleTable.clearSelection();
                    this.multipleList = [];
                }

            },

            handleSizeChange (val) {//每页显示条数触发事件
                this.pagesize = val;
                this.pageindex = 1;
                this.search();
            },

            handleCurrentChange (val) {//分页触发事件
                this.search();
            },
            checkSelect (selection, row) {//点击单选select触发事件
                this.multipleList = selection;
            },
            checkall (selection) {//点击全选select触发事件
                this.multipleList = selection;
            },

            cancel () {
               this.$emit('close',{type:'add',cancel:true})
            }

        }
    }
</script>

<style lang="less" scoped>

	.box {
		overflow: auto;
		height:500px;
		max-height: 500px;
	}
    .left, .right {
        // display: inline-block;
        float: left;
        vertical-align: top;
    }
    .left{
        width: 70%;
        min-width:440px;
    }
    .right {
        // width: 240px;
        width: 25%;
        height: 100%;
        margin-left:20px;
        overflow: auto;
        position: relative;
        
        .choose {
            box-shadow:0px 0px 10px #969696;
            border-radius:10px;
        }

        .first {
    		display:flex;
            align-items:center;
            justify-content: space-between;
            padding:10px 20px; 
        	width: 100%;
            background: #EEF1F4;

            .number {
                min-width: 10px;
                padding:2px 7px;
                font-weight: 700;
                border-radius:10px;
                background: #969c9c;
                color: #fff;
            }
        }
        .header{
            li{
                display:flex;
                align-items:center;
                justify-content: space-between;
                padding:10px 20px; 
                
                .del {
                    cursor: pointer;
                    position: relative;

                    &:hover i {
                        position: absolute;
                        left:50%;top:50%;
                        transform: translate(-50%, -50%);
                        width: 25px;
                        height: 25px;
                        border-radius: 50%;
                        background: rgba(0,0,0,0.2);
                    }
                }

            }

            
            
        }
    }
	.input {
		width:200px;
	}
	.btn {
		width:80px;
		margin-left:20px;
		padding:7px 15px;
	}

	.admin-table {
		margin-top:10px;
	}

	h2{
		line-height: 30px;
		text-align: center;
	}

	.class-page {
		margin-top:10px;
	}
	
	.submit {
		// float: right;
		display: block;
		margin:0 auto;
		margin-top:20px;
	}

</style>